package com.tc.dispatch.web.schedule.order;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import com.tc.dispatch.gsservice.ejb.GSServiceLocal;
import com.tc.dispatch.gsservice.exception.GSException;
import com.tc.dispatch.gsservice.model.ProcessStep;
import com.tc.dispatch.web.gsservice.gsHelper;
import com.tc.dispatch.web.util.DateJsonValueProcessor;

public class orderProcessRemove extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public orderProcessRemove() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html; charset=utf-8");
		// response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		String action = request.getParameter("action");
		if (action == null)
			action = "";

		GSServiceLocal gss = gsHelper.getGSServiceLocal();
		HttpSession sess = request.getSession();
		if (action.equals("list")) {
			/* 将所有的stepList显示出来 */

			List processList, processListDef;
			try {
				String start = request.getParameter("start");
				String limit = request.getParameter("limit");
				if (start == null)
					start = "0";
				if (limit == null)
					limit = "0";
				List ret = new ArrayList();
				ret = gss.findProcessStepByPages(Integer.parseInt(start),
						Integer.parseInt(limit));

				JSONObject json = new JSONObject();
				JsonConfig cfg = new JsonConfig();
				cfg.registerJsonValueProcessor(java.util.Date.class,
						new DateJsonValueProcessor());
				cfg.registerJsonValueProcessor(java.sql.Date.class,
						new DateJsonValueProcessor());

				// BeanUtils.copyProperties();
				json.put("totalProperty", new Integer(gss.getPSTotalNum("")));

				JSONArray arr = JSONArray.fromObject(ret.toArray(), cfg);
				json.put("root", arr);

				response.getWriter().print(json);
			} catch (Exception e) {
				out.print("{success:false, data:'false'}");
			}
		} 

		out.flush();
		out.close();
	}
	
	public boolean removeScheme(String specName,String scheme)
	{
		GSServiceLocal gss=gsHelper.getGSServiceLocal();
		try{
		List psList=gss.findProcessStep(" where specName='"+specName+"' and scheme="+scheme);
		Iterator it=psList.iterator();
		ProcessStep ps;
		while(it.hasNext())
		{
			ps=(ProcessStep)it.next();
			gss.removeProcessStep(ps.getId());
		}
		}
		catch(GSException gse)
		{
			System.out.println("removeScheme error is:"+gse.getMessage());
			return false;
		}
		return true;
	}
	public boolean removeSpec(String specName)
	{
		GSServiceLocal gss=gsHelper.getGSServiceLocal();
		try{
		List psList=gss.findProcessStep(" where specName='"+specName+"'");
		Iterator it=psList.iterator();
		ProcessStep ps;
		while(it.hasNext())
		{
			ps=(ProcessStep)it.next();
			gss.removeProcessStep(ps.getId());
		}
		}
		catch(GSException gse)
		{
			System.out.println("removeSpec error is:"+gse.getMessage());
			return false;
		}
		return true;
		
	}
	

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
